package com.ravi.controller;

import com.ravi.common.R;
import com.ravi.entity.DoctorUser;
import com.ravi.request.DoctorUserForm;
import com.ravi.service.DoctorUserService;
import com.ravi.util.jwt.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/doc")
@CrossOrigin
@Slf4j
public class DoctorUserController {

	@Autowired
	private DoctorUserService doctorUserService;

	@PostMapping("/login")
	public R login(@RequestBody DoctorUserForm userForm) {
		DoctorUser loginUser = new DoctorUser();
		loginUser.setWorkId(userForm.getWorkId());
		loginUser.setPassword(userForm.getPassword());
		log.info("当前登录医生的信息: [{}]",loginUser);

		try{
			DoctorUser userDB = doctorUserService.login(loginUser);
			Map<String,String> paras = new HashMap<>();
			paras.put("workId",userDB.getWorkId());
			paras.put("areaId", String.valueOf(userDB.getAreaId()));
			paras.put("roomId", String.valueOf(userDB.getRoomId()));
			String token = JWTUtils.getAccessToken(paras);
			return R.ok().put("user", userDB).put("token", token);
		}catch (Exception e) {
			return R.error(e.getMessage());
		}
	}
}
